package dataDAOImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import dataConnector.Connector;
import dataDAOInterfaces.DALException;
import dataDAOInterfaces.IProductBatchDAO;
import dataDTO.ProductBatchDTO;

public class MySQLProductBatchDAO implements IProductBatchDAO {

	@Override
	public ProductBatchDTO getProductBatch(int pbId) throws DALException {
		ResultSet rs = Connector.doQuery("SELECT * FROM produktbatch WHERE pb_id = " + pbId);
		try {
			if(!rs.first())
			throw new DALException("The produktbatch " + pbId + " dont exist");	
			return new ProductBatchDTO (rs.getInt(1), rs.getInt(2),rs.getInt(3));
	}
		catch (SQLException e) {throw new DALException(e); }
	}

	@Override
	public List<ProductBatchDTO> getProductBatchList() throws DALException {
		List<ProductBatchDTO> list = new ArrayList<ProductBatchDTO>();
		ResultSet rs = Connector.doQuery("SELECT*FROM produktbatch");
		try{
			while(rs.next())
			{
				list.add(new ProductBatchDTO(rs.getInt(1),rs.getInt(2),rs.getInt(3)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}

	@Override
	public void createProductBatch(ProductBatchDTO produktbatch)
			throws DALException {
		Connector.doUpdate("INSERT INTO produktbatch(pb_id, status, recept_id) VALUES" 
			+ "(" + produktbatch.getPbId() + ",'" + produktbatch.getStatus() + "','"
				+ produktbatch.getRecipeId() + "')");		
	}

	@Override
	public void updateProductBatch(ProductBatchDTO productbatch)
			throws DALException {
		Connector.doUpdate("UPDATE produktbatch SET pb_id = '" + productbatch.getPbId() +
				"', status = '" + productbatch.getStatus() + "', recept_id = '" + productbatch.getRecipeId() +
				"' WHERE pb_id = " + productbatch.getPbId());
		
	}

}
